// const webpack = require('webpack')
const path = require('path')
const { merge } = require('webpack-merge')
const webpackCommon = require('./webpack.common.js')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const TerserWebpackPluin = require('terser-webpack-plugin')

module.exports = merge(webpackCommon, {
  mode: 'production',
  module: {
    rules: [
      {
        test: /\.css$/,
        exclude: '/node_modules',
        use: [
          {
            loader: 'style-loader'
          },
          // MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader'
          }
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, 'public/favicon.ico'),
          to: './',
          globOptions: {
            ignore: [
              '**/*.ejs'
            ]
          }
        }
      ]
    }),
    new MiniCssExtractPlugin({
      filename: '[name]-[hash:8].bundle.css'
    })
  ],
  optimization: {
    sideEffects: true, // 一般用于npm包标记是否有副作用
    // 模块只导出被使用的成员
    usedExports: true, // 只使用外部使用了的export成员,
    // 压缩输出结果
    minimize: true,
    // 尽可能合并每一个模块到函数中
    concatenateModules: true,
    splitChunks: {
      chunks: 'all'
    },
    minimizer: [
      new TerserWebpackPluin(),
      new OptimizeCssAssetsWebpackPlugin()
    ]
  }
})
